﻿using Artemis;
using libtcod;
using RL.Components;

namespace RL.Systems
{
    public class PlayerKeyboardControlSystem : TagSystem
    {
        private ComponentMapper<Position> _positionMapper;
        private TCODKey _key;

        public PlayerKeyboardControlSystem(string tag) : base(tag)
        {
            
        }        

        public override void Initialize()
        {
            _positionMapper = new ComponentMapper<Position>(world);
        }

        public override void Process(Artemis.Entity e)
        {
            _key = TCODConsole.checkForKeypress((int)TCODKeyStatus.KeyPressed);
            if (_key.Character == '\0') return;

            Logger.WriteInfo(string.Format("InputEngine: Key press ({0})", _key.Character));

            var pos = _positionMapper.Get(e);

            if (_key.KeyCode == TCODKeyCode.Escape)
            {
                SettlersRL.Quit = true;
            }

            switch (_key.Character )
            {
                case 'a':
                    pos.X -= 1;
                    break;
                case 'd':
                    pos.X += 1;
                    break;
                case 'w':
                    pos.Y -= 1;
                    break;
                case 's':
                    pos.Y += 1;
                    break;
            }
        }
    }
}